/*-------------------------------------------------------------------------
 *
 *	  EUC_JP, SJIS 和 MULE_INTERNAL
 *
 * Portions Copyright (c) 1996-2022, PostgreSQL Global Development Group
 * Portions Copyright (c) 1994, Regents of the University of California
 *
 * IDENTIFICATION
 *	  src/backend/utils/mb/conversion_procs/euc_jp_and_sjis/euc_jp_and_sjis.c
 *
 *-------------------------------------------------------------------------
 */

#include "postgres.h"
#include "fmgr.h"
#include "mb/pg_wchar.h"

/* 
 * SJIS 替代代码。
 * 如果没有定义 EUC -> SJIS 的映射，则使用此代码。
 */
#define PGSJISALTCODE 0x81ac
#define PGEUCALTCODE 0xa2ae

/* 
 * SJIS UDC (IBM 汉字) 和 EUC_JP 之间的转换表
 */
#include "sjis.map"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(euc_jp_to_sjis);
PG_FUNCTION_INFO_V1(sjis_to_euc_jp);
PG_FUNCTION_INFO_V1(euc_jp_to_mic);
PG_FUNCTION_INFO_V1(mic_to_euc_jp);
PG_FUNCTION_INFO_V1(sjis_to_mic);
PG_FUNCTION_INFO_V1(mic_to_sjis);

/* ----------
 * conv_proc(
 *		INTEGER,	-- 源编码id
 *		INTEGER,	-- 目标编码id
 *		CSTRING,	-- 源字符串（以null结尾的C字符串）
 *		CSTRING,	-- 目标字符串（以null结尾的C字符串）
 *		INTEGER,	-- 源字符串长度
 *		BOOL		-- 如果为真，转换失败时不抛出错误
 * ) 返回INTEGER;
 *
 * 返回成功转换的字节数。
 * ----------
 */

static int	fc_sjis2mic(const unsigned char *fc_sjis, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_mic2sjis(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_euc_jp2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_mic2euc_jp(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_euc_jp2sjis(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);
static int	fc_sjis2euc_jp(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError);

Datum euc_jp_to_sjis(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_SJIS);

	fc_converted = fc_euc_jp2sjis(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum sjis_to_euc_jp(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_EUC_JP);

	fc_converted = fc_sjis2euc_jp(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum euc_jp_to_mic(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_EUC_JP, PG_MULE_INTERNAL);

	fc_converted = fc_euc_jp2mic(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum mic_to_euc_jp(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_EUC_JP);

	fc_converted = fc_mic2euc_jp(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum sjis_to_mic(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_SJIS, PG_MULE_INTERNAL);

	fc_converted = fc_sjis2mic(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

Datum mic_to_sjis(PG_FUNCTION_ARGS)
{
	unsigned char *fc_src = (unsigned char *) PG_GETARG_CSTRING(2);
	unsigned char *fc_dest = (unsigned char *) PG_GETARG_CSTRING(3);
	int			fc_len = PG_GETARG_INT32(4);
	bool		fc_noError = PG_GETARG_BOOL(5);
	int			fc_converted;

	CHECK_ENCODING_CONVERSION_ARGS(PG_MULE_INTERNAL, PG_SJIS);

	fc_converted = fc_mic2sjis(fc_src, fc_dest, fc_len, fc_noError);

	PG_RETURN_INT32(fc_converted);
}

/* 
 * SJIS ---> MIC
 */
static int fc_sjis2mic(const unsigned char *fc_sjis, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_sjis;
	int			fc_c1,
				fc_c2,
				fc_i,
				fc_k,
				fc_k2;

	while (fc_len > 0)
	{
		fc_c1 = *fc_sjis;
		if (fc_c1 >= 0xa1 && fc_c1 <= 0xdf)
		{
			/* JIS X0201 (1 字节平假名) */
			*fc_p++ = LC_JISX0201K;
			*fc_p++ = fc_c1;
			fc_sjis++;
			fc_len--;
		}
		else if (IS_HIGHBIT_SET(fc_c1))
		{
			/* 
			 * JIS X0208, X0212, 用户定义的扩展字符
 */
			if (fc_len < 2 || !ISSJISHEAD(fc_c1) || !ISSJISTAIL(fc_sjis[1]))
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_SJIS, (const char *) fc_sjis, fc_len);
			}
			fc_c2 = fc_sjis[1];
			fc_k = (fc_c1 << 8) + fc_c2;
			if (fc_k >= 0xed40 && fc_k < 0xf040)
			{
				/* NEC 选择 IBM 汉字 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].nec;
					if (fc_k2 == 0xffff)
						break;
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].sjis;
						fc_c1 = (fc_k >> 8) & 0xff;
						fc_c2 = fc_k & 0xff;
					}
				}
			}

			if (fc_k < 0xeb3f)
			{
				/* JIS X0208 */
				*fc_p++ = LC_JISX0208;
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0x9f + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if ((fc_k >= 0xeb40 && fc_k < 0xf040) || (fc_k >= 0xfc4c && fc_k <= 0xfcfc))
			{
				/* NEC 选择 IBM 汉字 - 其他未决正义 */
				*fc_p++ = LC_JISX0208;
				*fc_p++ = PGEUCALTCODE >> 8;
				*fc_p++ = PGEUCALTCODE & 0xff;
			}
			else if (fc_k >= 0xf040 && fc_k < 0xf540)
			{
				/* 
				 * UDC1 映射到 X0208 85 ku - 94 ku JIS 代码 0x7521 -
				 * 0x7e7e EUC 0xf5a1 - 0xfefe
 */
				*fc_p++ = LC_JISX0208;
				fc_c1 -= 0x6f;
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0xf3 + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if (fc_k >= 0xf540 && fc_k < 0xfa40)
			{
				/* 
				 * UDC2 映射到 X0212 85 ku - 94 ku JIS 代码 0x7521 -
				 * 0x7e7e EUC 0x8ff5a1 - 0x8ffefe
 */
				*fc_p++ = LC_JISX0212;
				fc_c1 -= 0x74;
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0xf3 + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if (fc_k >= 0xfa40)
			{
				/* 
				 * IBM 汉字映射到 X0208 和 X0212
 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].sjis;
					if (fc_k2 == 0xffff)
						break;
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].euc;
						if (fc_k >= 0x8f0000)
						{
							*fc_p++ = LC_JISX0212;
							*fc_p++ = 0x80 | ((fc_k & 0xff00) >> 8);
							*fc_p++ = 0x80 | (fc_k & 0xff);
						}
						else
						{
							*fc_p++ = LC_JISX0208;
							*fc_p++ = 0x80 | (fc_k >> 8);
							*fc_p++ = 0x80 | (fc_k & 0xff);
						}
					}
				}
			}
			fc_sjis += 2;
			fc_len -= 2;
		}
		else
		{						/* 应该是 ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_SJIS, (const char *) fc_sjis, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_sjis++;
			fc_len--;
		}
	}
	*fc_p = '\0';

	return fc_sjis - fc_start;
}

/* 
 * MIC ---> SJIS
 */
static int fc_mic2sjis(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_mic;
	int			fc_c1,
				fc_c2,
				fc_k,
				fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_mic;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_MULE_INTERNAL,
										(const char *) fc_mic, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_mic++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) fc_mic, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_MULE_INTERNAL,
									(const char *) fc_mic, fc_len);
		}
		if (fc_c1 == LC_JISX0201K)
			*fc_p++ = fc_mic[1];
		else if (fc_c1 == LC_JISX0208)
		{
			fc_c1 = fc_mic[1];
			fc_c2 = fc_mic[2];
			fc_k = (fc_c1 << 8) | (fc_c2 & 0xff);
			if (fc_k >= 0xf5a1)
			{
				/* UDC1 */
				fc_c1 -= 0x54;
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1) + 0x6f;
			}
			else
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1);
			*fc_p++ = fc_c2 - ((fc_c1 & 1) ? ((fc_c2 < 0xe0) ? 0x61 : 0x60) : 2);
		}
		else if (fc_c1 == LC_JISX0212)
		{
			int			fc_i,
						fc_k2;

			fc_c1 = fc_mic[1];
			fc_c2 = fc_mic[2];
			fc_k = fc_c1 << 8 | fc_c2;
			if (fc_k >= 0xf5a1)
			{
				/* UDC2 */
				fc_c1 -= 0x54;
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1) + 0x74;
				*fc_p++ = fc_c2 - ((fc_c1 & 1) ? ((fc_c2 < 0xe0) ? 0x61 : 0x60) : 2);
			}
			else
			{
				/* IBM 汉字 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].euc & 0xffff;
					if (fc_k2 == 0xffff)
					{
						*fc_p++ = PGSJISALTCODE >> 8;
						*fc_p++ = PGSJISALTCODE & 0xff;
						break;
					}
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].sjis;
						*fc_p++ = fc_k >> 8;
						*fc_p++ = fc_k & 0xff;
						break;
					}
				}
			}
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_MULE_INTERNAL, PG_SJIS,
									   (const char *) fc_mic, fc_len);
		}
		fc_mic += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_mic - fc_start;
}

/* 
 * EUC_JP ---> MIC
 */
static int fc_euc_jp2mic(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_euc;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_euc;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_JP,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_euc++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_EUC_JP, (const char *) fc_euc, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_EUC_JP,
									(const char *) fc_euc, fc_len);
		}
		if (fc_c1 == SS2)
		{						/* 1 字节的假名？ */
			*fc_p++ = LC_JISX0201K;
			*fc_p++ = fc_euc[1];
		}
		else if (fc_c1 == SS3)
		{						/* JIS X0212 汉字? */
			*fc_p++ = LC_JISX0212;
			*fc_p++ = fc_euc[1];
			*fc_p++ = fc_euc[2];
		}
		else
		{						/* 汉字？ */
			*fc_p++ = LC_JISX0208;
			*fc_p++ = fc_c1;
			*fc_p++ = fc_euc[1];
		}
		fc_euc += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_euc - fc_start;
}

/* 
 * MIC ---> EUC_JP
 */
static int fc_mic2euc_jp(const unsigned char *fc_mic, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_mic;
	int			fc_c1;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_mic;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_MULE_INTERNAL,
										(const char *) fc_mic, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_mic++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_MULE_INTERNAL, (const char *) fc_mic, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_MULE_INTERNAL,
									(const char *) fc_mic, fc_len);
		}
		if (fc_c1 == LC_JISX0201K)
		{
			*fc_p++ = SS2;
			*fc_p++ = fc_mic[1];
		}
		else if (fc_c1 == LC_JISX0212)
		{
			*fc_p++ = SS3;
			*fc_p++ = fc_mic[1];
			*fc_p++ = fc_mic[2];
		}
		else if (fc_c1 == LC_JISX0208)
		{
			*fc_p++ = fc_mic[1];
			*fc_p++ = fc_mic[2];
		}
		else
		{
			if (fc_noError)
				break;
			report_untranslatable_char(PG_MULE_INTERNAL, PG_EUC_JP,
									   (const char *) fc_mic, fc_len);
		}
		fc_mic += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_mic - fc_start;
}

/* 
 * EUC_JP -> SJIS
 */
static int fc_euc_jp2sjis(const unsigned char *fc_euc, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_euc;
	int			fc_c1,
				fc_c2,
				fc_k;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_euc;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_EUC_JP,
										(const char *) fc_euc, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_euc++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_EUC_JP, (const char *) fc_euc, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_EUC_JP,
									(const char *) fc_euc, fc_len);
		}
		if (fc_c1 == SS2)
		{
			/* 半角平假名? */
			*fc_p++ = fc_euc[1];
		}
		else if (fc_c1 == SS3)
		{
			/* JIS X0212 汉字? */
			fc_c1 = fc_euc[1];
			fc_c2 = fc_euc[2];
			fc_k = fc_c1 << 8 | fc_c2;
			if (fc_k >= 0xf5a1)
			{
				/* UDC2 */
				fc_c1 -= 0x54;
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1) + 0x74;
				*fc_p++ = fc_c2 - ((fc_c1 & 1) ? ((fc_c2 < 0xe0) ? 0x61 : 0x60) : 2);
			}
			else
			{
				int			fc_i,
							fc_k2;

				/* IBM 汉字 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].euc & 0xffff;
					if (fc_k2 == 0xffff)
					{
						*fc_p++ = PGSJISALTCODE >> 8;
						*fc_p++ = PGSJISALTCODE & 0xff;
						break;
					}
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].sjis;
						*fc_p++ = fc_k >> 8;
						*fc_p++ = fc_k & 0xff;
						break;
					}
				}
			}
		}
		else
		{
			/* JIS X0208 汉字? */
			fc_c2 = fc_euc[1];
			fc_k = (fc_c1 << 8) | (fc_c2 & 0xff);
			if (fc_k >= 0xf5a1)
			{
				/* UDC1 */
				fc_c1 -= 0x54;
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1) + 0x6f;
			}
			else
				*fc_p++ = ((fc_c1 - 0xa1) >> 1) + ((fc_c1 < 0xdf) ? 0x81 : 0xc1);
			*fc_p++ = fc_c2 - ((fc_c1 & 1) ? ((fc_c2 < 0xe0) ? 0x61 : 0x60) : 2);
		}
		fc_euc += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_euc - fc_start;
}

/*
 * SJIS ---> EUC_JP
 */
static int fc_sjis2euc_jp(const unsigned char *fc_sjis, unsigned char *fc_p, int fc_len, bool fc_noError)
{
	const unsigned char *fc_start = fc_sjis;
	int			fc_c1,
				fc_c2,
				fc_i,
				fc_k,
				fc_k2;
	int			fc_l;

	while (fc_len > 0)
	{
		fc_c1 = *fc_sjis;
		if (!IS_HIGHBIT_SET(fc_c1))
		{
			/* ASCII */
			if (fc_c1 == 0)
			{
				if (fc_noError)
					break;
				report_invalid_encoding(PG_SJIS,
										(const char *) fc_sjis, fc_len);
			}
			*fc_p++ = fc_c1;
			fc_sjis++;
			fc_len--;
			continue;
		}
		fc_l = pg_encoding_verifymbchar(PG_SJIS, (const char *) fc_sjis, fc_len);
		if (fc_l < 0)
		{
			if (fc_noError)
				break;
			report_invalid_encoding(PG_SJIS,
									(const char *) fc_sjis, fc_len);
		}
		if (fc_c1 >= 0xa1 && fc_c1 <= 0xdf)
		{
			/* JIS X0201 (1 字节平假名) */
			*fc_p++ = SS2;
			*fc_p++ = fc_c1;
		}
		else
		{
			/* 
			 * JIS X0208, X0212, 用户定义的扩展字符
 */
			fc_c2 = fc_sjis[1];
			fc_k = (fc_c1 << 8) + fc_c2;
			if (fc_k >= 0xed40 && fc_k < 0xf040)
			{
				/* NEC 选择 IBM 汉字 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].nec;
					if (fc_k2 == 0xffff)
						break;
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].sjis;
						fc_c1 = (fc_k >> 8) & 0xff;
						fc_c2 = fc_k & 0xff;
					}
				}
			}

			if (fc_k < 0xeb3f)
			{
				/* JIS X0208 */
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0x9f + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if ((fc_k >= 0xeb40 && fc_k < 0xf040) || (fc_k >= 0xfc4c && fc_k <= 0xfcfc))
			{
				/* NEC 选择 IBM 汉字 - 其他未决正义 */
				*fc_p++ = PGEUCALTCODE >> 8;
				*fc_p++ = PGEUCALTCODE & 0xff;
			}
			else if (fc_k >= 0xf040 && fc_k < 0xf540)
			{
				/* 
				 * UDC1 映射到 X0208 85 ku - 94 ku JIS 代码 0x7521 -
				 * 0x7e7e EUC 0xf5a1 - 0xfefe
 */
				fc_c1 -= 0x6f;
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0xf3 + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if (fc_k >= 0xf540 && fc_k < 0xfa40)
			{
				/* 
				 * UDC2 映射到 X0212 85 ku - 94 ku JIS 代码 0x7521 -
				 * 0x7e7e EUC 0x8ff5a1 - 0x8ffefe
 */
				*fc_p++ = SS3;
				fc_c1 -= 0x74;
				*fc_p++ = ((fc_c1 & 0x3f) << 1) + 0xf3 + (fc_c2 > 0x9e);
				*fc_p++ = fc_c2 + ((fc_c2 > 0x9e) ? 2 : 0x60) + (fc_c2 < 0x80);
			}
			else if (fc_k >= 0xfa40)
			{
				/*
				 * 将 IBM 汉字映射到 X0208 和 X0212
				 *
				 */
				for (fc_i = 0;; fc_i++)
				{
					fc_k2 = ibmkanji[fc_i].sjis;
					if (fc_k2 == 0xffff)
						break;
					if (fc_k2 == fc_k)
					{
						fc_k = ibmkanji[fc_i].euc;
						if (fc_k >= 0x8f0000)
						{
							*fc_p++ = SS3;
							*fc_p++ = 0x80 | ((fc_k & 0xff00) >> 8);
							*fc_p++ = 0x80 | (fc_k & 0xff);
						}
						else
						{
							*fc_p++ = 0x80 | (fc_k >> 8);
							*fc_p++ = 0x80 | (fc_k & 0xff);
						}
					}
				}
			}
		}
		fc_sjis += fc_l;
		fc_len -= fc_l;
	}
	*fc_p = '\0';

	return fc_sjis - fc_start;
}
